package nr.co.mhgames.polyanim.util;

import java.awt.Color;

/**
 * Simple mutable color definition that can be modified, unlike the immutable
 * AWT Color. Public members for user convinience and performance.
 * 
 * @author Mika Halttunen
 */
public final class ColorDef {
	public int red;
	public int green;
	public int blue;
	public int alpha;

	public ColorDef() {
		red = green = blue = alpha = 0;
	}

	public ColorDef(int r, int g, int b, int a) {
		set(r, g, b, a);
	}

	public ColorDef(int r, int g, int b) {
		set(r, g, b, 255);
	}

	public ColorDef(float r, float g, float b, float a) {
		set(r, g, b, a);
	}

	public ColorDef(float r, float g, float b) {
		set(r, g, b, 1.0f);
	}

	public ColorDef(Color color) {
		setFrom(color);
	}

	public void setFrom(Color color) {
		red = color.getRed();
		green = color.getGreen();
		blue = color.getBlue();
		alpha = color.getAlpha();
	}

	public void setFrom(ColorDef color) {
		red = color.red;
		green = color.green;
		blue = color.blue;
		alpha = color.alpha;
	}

	public void set(int r, int g, int b, int a) {
		red = r;
		green = g;
		blue = b;
		alpha = a;
	}

	public void set(float r, float g, float b, float a) {
		red = (int) (r * 255.0f);
		green = (int) (g * 255.0f);
		blue = (int) (b * 255.0f);
		alpha = (int) (a * 255.0f);
	}


	/**
	 * @see {@link Color#getRGBComponents(float[])}
	 */
	public float[] getRGBComponents(float out[]) {
		if (out == null) {
			out = new float[4];
		}

		out[0] = (float) red / 255.0f;
		out[1] = (float) green / 255.0f;
		out[2] = (float) blue / 255.0f;
		out[3] = (float) alpha / 255.0f;

		return out;
	}
}
